package com.example.smartline.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartline.R;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private TextView tv1;//item.xml里的TextView：Textviewname
    private TextView tv2;//item.xml里的TextView：Textviewage
    private EditText keywordEdit;
    private Button searchButton;//activity_main.xml里的Button
    private ListView lv;//activity_main.xml里的ListView
    private BaseAdapter adapter;//要实现的类
    private ArrayList<String> name ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = new ArrayList<String>();

        lv = (ListView) findViewById(R.id.listView1);
        searchButton = (Button)findViewById(R.id.searchButton);
        keywordEdit = (EditText) findViewById(R.id.keywordEdit);

        initData();
        initBaseAdapter();
        initListViewItemOnClick();

    }

    //初始化数据
    public void initData(){
        for (int i = 0 ; i < 10 ; i++){
            name.add("功能"+i);
        }
    }

    //初始化适配器
    public void initBaseAdapter(){
        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return name.size();//数目
            }
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = MainActivity.this.getLayoutInflater();
                View view;

                if (convertView==null) {
                    //因为getView()返回的对象，adapter会自动赋给ListView
                    view = inflater.inflate(R.layout.test_lv_item, null);
                }else{
                    view=convertView;
                    Log.i("info","有缓存，不需要重新生成"+position);
                }
                tv1 = (TextView) view.findViewById(R.id.tv_name);//找到Textviewname
                tv1.setText(name.get(position));//设置参数
                return view;
            }
            @Override
            public long getItemId(int position) {//取在列表中与指定索引对应的行id
                return 0;
            }
            @Override
            public Object getItem(int position) {//获取数据集中与指定索引对应的数据项
                return null;
            }
        };
        lv.setAdapter(adapter);

    }

    //监听listview点击事件
    public void initListViewItemOnClick(){
        //获取当前ListView点击的行数，并且得到该数据
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv1 = (TextView) view.findViewById(R.id.tv_name);//找到Textviewname
                Toast.makeText(MainActivity.this, "" + tv1.getText().toString(), Toast.LENGTH_SHORT).show();//显示数据
                jumpActivity(tv1.getText().toString());
            }
        });

    }

    public void jumpActivity(String name){
        Intent it = new Intent(MainActivity.this, com.example.smartline.activity.MapDemoActivity.class); //
        startActivity(it);
    }
}
